﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Catalyst.Web.DynamicData
{
    /// <summary>
    /// Extends ParameterCollection to allow the use of extended Parameters.  Currently the 
    /// DynamicParameterCollection is limited to the following parameters:
    ///     System.Web.UI.WebControls.Parameter
    ///     System.Web.UI.WebControls.ControlParameter
    ///     System.Web.UI.WebControls.CookieParameter
    ///     System.Web.UI.WebControls.FormParameter
    ///     System.Web.UI.WebControls.ProfileParameter
    ///     System.Web.UI.WebControls.QueryStringParameter
    ///     System.Web.UI.WebControls.SessionParameter
    ///     Catalyst.Web.DynamicData.AndExpressionParameter
    ///     Catalyst.Web.DynamicData.OrExpressionParameter
    ///     Catalyst.Web.DynamicData.InExpressionParameter
    ///     Catalyst.Web.DynamicData.LikeExpressionParameter
    ///     Catalyst.Web.DynamicData.RangeExpressionParameter
    ///     Catalyst.Web.DynamicData.EqualsExpressionParameter
    ///     Catalyst.Web.DynamicData.NotNullOrEmptyParameter
    /// This class should be modified to accept any object that extends Parameter.
    /// </summary>
    public class DynamicParameterCollection : ParameterCollection
    {
        private readonly Type[] _knownTypes = new Type[] { typeof(AndExpressionParameter), 
                                                            typeof(OrExpressionParameter), 
                                                            typeof(InExpressionParameter), 
                                                            typeof(LikeExpressionParameter), 
                                                            typeof(RangeExpressionParameter),
                                                            typeof(EqualsExpressionParameter),
                                                            typeof(NotNullOrEmptyParameter) };
        
        protected override object CreateKnownType(int index)
        {
            int baseTypesLength = base.GetKnownTypes().Length;
            if(index < baseTypesLength)
                return base.CreateKnownType(index);

            return Activator.CreateInstance(_knownTypes[index - baseTypesLength]);
        }
        
        protected override Type[] GetKnownTypes()
        {
            List<Type> knownTypes = new List<Type>(base.GetKnownTypes());
            knownTypes.AddRange(_knownTypes);
            return knownTypes.ToArray();
        }

    }
}
